package com.taylor.community.quartz;

import com.taylor.community.util.CommunityConstant;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;

public class WkImageDeleteJob implements Job, CommunityConstant {
    private static final Logger logger = LoggerFactory.getLogger(WkImageDeleteJob.class);
    @Value("${wk.image.storage}")
    private String wkImageStorage;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        File[] files = new File(wkImageStorage).listFiles();//获取该文件夹的所有文件
        if (files.length == 0){
            logger.info("【任务取消】没有WK生成的长图！");
            return;
        }
        for (File file : files){
            //删除一分钟前生成的图片 file.lastModified()返回最后一次修改文件的时间
            if (System.currentTimeMillis() - file.lastModified() > 60 * 1000){
                file.delete();
                logger.info("【任务开始】删除WK生成的图片："+file.getName());
            }
        }
    }
}
